package com.zjun.demo.experimentaldemo.share

import android.content.Intent
import android.net.Uri
import android.os.Build
import android.util.Log
import com.zjun.demo.experimentaldemo.R
import com.zjun.demo.experimentaldemo.base.BaseActivity
import com.zjun.demo.experimentaldemo.databinding.ActivityReceiveSharingBinding
import com.zjun.demo.experimentaldemo.utils.UriUtils

/**
 * Android 接收微信、QQ其他应用打开，第三方分享
 */
class ReceiveSharingActivity : BaseActivity<ActivityReceiveSharingBinding>(){

    override fun getLayoutId(): Int = R.layout.activity_receive_sharing

    override fun initialize() {
        receiveActionSend(intent)
    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        val data = intent.extras?.getString("EXT") ?: ""
        Log.i("", "onNewIntent: $data")
        receiveActionSend(intent)
    }

    private fun receiveActionSend(intent: Intent) {
        val uris = mutableListOf<Uri>()
        intent.type ?: return
        when (intent.action) {

            Intent.ACTION_SEND_MULTIPLE -> {
                val fileUris = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                    intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM, Uri::class.java)
                } else {
                    intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM)
                }
                if(fileUris!=null){
                    uris.addAll(fileUris)
                }
            }

            Intent.ACTION_SEND -> {
                val uri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                    intent.getParcelableExtra(Intent.EXTRA_STREAM, Uri::class.java)
                } else {
                    intent.getParcelableExtra(Intent.EXTRA_STREAM)
                }
                uris.add(uri!!)
            }
        }
        for (uri in uris){
            val path = UriUtils.getFileFromUri(this,uri)
        }
    }

}